package com.hwd.gmall.seckill.listener;

import com.hwd.gmall.common.aspect.ListenMessage;
import com.hwd.gmall.common.constant.SeckillConst;
import com.hwd.gmall.seckill.service.SeckillGoodsService;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 商品数据同步消息的消费者
 *
 * @author 黄伟东/Victor
 * @date 2022/5/12 21:19
 */
@Component
@Log4j2
public class SeckillGoodsListener {

    @Resource
    private SeckillGoodsService seckillGoodsService;

    /**
     * 监听延迟小时，活动结束后同步商品数据到数据库
     *
     * @param message        消息体
     * @param ignoredChannel 连接
     */
    @RabbitListener(queues = SeckillConst.SECKILL_GOODS_NORMAL_QUEUE)
    @ListenMessage(messageDescription = "商品数据同步的消息")
    public void seckillOrderAdd(Message message, Channel ignoredChannel) {
        // 商品数据同步
        seckillGoodsService.mergeSeckillGoodsStockFromRedisToDb(new String(message.getBody()));
    }

}
